package org.example;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.BitSet;

public class Main2 {
    public static void main(String[] args) {
        CharStream input = CharStreams.fromString("count");
        com.neuronbit.parser.DemoLexer lexer = new com.neuronbit.parser.DemoLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        com.neuronbit.parser.DemoParser parser = new com.neuronbit.parser.DemoParser(tokens);

        parser.addErrorListener(new BaseErrorListener() {
            @Override
            public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
                super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);

//                this.partialFruit = null;
                String[] typeAssistTokens = new String[]{"Fruits"};
                TokenStream tokenStream = parser.getTokenStream();

                System.out.println(tokenStream.size());
                // last token is always "fake" EOF token
                if (tokenStream.size() > 0) {
                    Token lastToken = tokenStream.get(tokenStream.size() - 1);
                    String tokenType = parser.getVocabulary().getDisplayName(lastToken.getType());
                    System.out.println(tokenType);
//                    if (typeAssistTokens.indexOf(tokenType) >= 0) {
//                        this.partialFruit = lastToken.text;
//                    }
                }
            }

            @Override
            public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
                super.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
            }

            @Override
            public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
                super.reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs);
            }

            @Override
            public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
                super.reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs);
            }
        });

        ParseTree root = parser.query();

//        CodeCompletionCore c3 = new CodeCompletionCore(parser, null, null);
//
//        CodeCompletionCore.CandidatesCollection candidates = c3.collectCandidates(0, null);
//        for (List<Integer> value : candidates.tokens.values()) {
//            System.out.println(parser.getVocabulary().getDisplayName(value.get(0)));
//        }


//        MysqlSqlDefaultListener listener = new MysqlSqlDefaultListener();
//
//        ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
//        walker.walk(listener, root);
//
//        SqlCreateTable table = listener.getTable();
    }
}
